//
//  AddWashCarViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/21.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "AddWashCarViewController.h"

@interface AddWashCarViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    UIView *bottomView;
    
}
@property (weak, nonatomic) IBOutlet UILabel *labSel;
@property (weak, nonatomic) IBOutlet UITextField *textIntro;

@property (weak, nonatomic) IBOutlet UITextField *textW1;
@property (weak, nonatomic) IBOutlet UITextField *textW2;
@property (weak, nonatomic) IBOutlet UITextField *textS1;
@property (weak, nonatomic) IBOutlet UITextField *textS2;
@property (weak, nonatomic) IBOutlet UITextField *textD1;
@property (weak, nonatomic) IBOutlet UITextField *textD2;
@property (weak, nonatomic) IBOutlet UIButton *btnAdd;

@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,copy)NSString *str;
@property (nonatomic,copy)NSString *strid;

@end

@implementation AddWashCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    _labSel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click1)];
    [_labSel addGestureRecognizer:tap1];
    [self loadData];
    [self loadUI];
    
}

- (void)loadUI{
    if ([_state isEqualToString:@"aaa"]) {//详情
        _labSel.userInteractionEnabled = NO;
        _textIntro.userInteractionEnabled = NO;
        _textW1.userInteractionEnabled = NO;
        _textW2.userInteractionEnabled = NO;
        _textS1.userInteractionEnabled = NO;
        _textS2.userInteractionEnabled = NO;
        _textD1.userInteractionEnabled = NO;
        _textD2.userInteractionEnabled = NO;
        _btnAdd.hidden = YES;
        [self showNav:@"商品详情"];
        _labSel.text = _name;
        _textIntro.text = [_dic objectForKey:@"intro"];
        _textW1.text = [_dic objectForKey:@"limousineprice"];
        _textW2.text = [_dic objectForKey:@"limousinediudiuprice"];
        _textS1.text = [_dic objectForKey:@"suvprice"];
        _textS2.text = [_dic objectForKey:@"suvdiudiuprice"];
        _textD1.text = [_dic objectForKey:@"taxiprice"];
        _textD2.text = [_dic objectForKey:@"taxidiudiuprice"];
        
        
    }else if ([_state isEqualToString:@"sss"]){//修改
        [self showNav:@"商品修改"];
        _labSel.text = _name;
        _textIntro.text = [_dic objectForKey:@"intro"];
        _textW1.text = [_dic objectForKey:@"limousineprice"];
        _textW2.text = [_dic objectForKey:@"limousinediudiuprice"];
        _textS1.text = [_dic objectForKey:@"suvprice"];
        _textS2.text = [_dic objectForKey:@"suvdiudiuprice"];
        _textD1.text = [_dic objectForKey:@"taxiprice"];
        _textD2.text = [_dic objectForKey:@"taxidiudiuprice"];
        [_btnAdd setTitle:@"修改" forState:UIControlStateNormal];
        
    }else{//添加
        [self showNav:@"添加商品"];
    }
}

- (void)click1{
    bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-250, ScreenWidth, 250)];
    bottomView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:bottomView];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 0, 40, 40);
    [btn1 setTitle:@"取消" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(ScreenWidth-50, 0, 40, 40);
    [btn2 setTitle:@"确定" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn2];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, ScreenWidth, 210)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [bottomView addSubview:pickerView];
    
}

- (void)clickCancel{
    [bottomView removeFromSuperview];
}

- (void)clickSure{
    if (_str.length > 0) {
        [bottomView removeFromSuperview];
        _labSel.text = _str;
    }
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return _array.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *str = [_array[row] objectForKey:@"name"];
    return str;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *str = [_array[row] objectForKey:@"name"];
    _strid = [_array[row] objectForKey:@"id"];
    _str = str;
    NSLog(@"str:%@",str);
}
//洗车-类别展示
- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    NSString *url = [NSString stringWithFormat:@"%@/App/CarWash/classes",urlHead];
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _array = [responseObject objectForKey:@"result"];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}

- (IBAction)clickAdd:(id)sender {
    if ([_labSel.text isEqualToString:@"请选择"]) {
        [self showAlert:@"请选择商品类型"];
        return;
    }else if (_textIntro.text.length ==0){
        [self showAlert:@"请输入商品简介"];
        return;
    }else if (_textW1.text.length == 0){
        [self showAlert:@"请输入原价"];
        return;
    }else if (_textW2.text.length == 0){
        [self showAlert:@"请输入现价"];
        return;
    }else if (_textS1.text.length == 0){
        [self showAlert:@"请输入原价"];
        return;
    }else if (_textS2.text.length == 0){
        [self showAlert:@"请输入现价"];
        return;
    }else if (_textD1.text.length == 0){
        [self showAlert:@"请输入原价"];
        return;
    }else if (_textD2.text.length == 0){
        [self showAlert:@"请输入现价"];
        return;
    }
    
    if ([_state isEqualToString:@"aaa"]) {//详情
        
    }else if ([_state isEqualToString:@"sss"]){//修改
        [self loadUpdata];
    }else{
        [self loadAdd];
    }
    
}

- (void)loadUpdata{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:_strid forKey:@"classid"];
    [parmas setObject:_textIntro.text forKey:@"intro"];
    [parmas setObject:_textW1.text forKey:@"limousineprice"];
    [parmas setObject:_textW2.text forKey:@"limousinediudiuprice"];
    [parmas setObject:_textS1.text forKey:@"suvprice"];
    [parmas setObject:_textS2.text forKey:@"suvdiudiuprice"];
    [parmas setObject:_textD1.text forKey:@"taxiprice"];
    [parmas setObject:_textD2.text forKey:@"taxidiudiuprice"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/CarWash/update",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"添加商品res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];

}

//添加商品
- (void)loadAdd{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_merid forKey:@"merchantid"];
    [parmas setObject:_strid forKey:@"classid"];
    [parmas setObject:_textIntro.text forKey:@"intro"];
    [parmas setObject:_textW1.text forKey:@"limousineprice"];
    [parmas setObject:_textW2.text forKey:@"limousinediudiuprice"];
    [parmas setObject:_textS1.text forKey:@"suvprice"];
    [parmas setObject:_textS2.text forKey:@"suvdiudiuprice"];
    [parmas setObject:_textD1.text forKey:@"taxiprice"];
    [parmas setObject:_textD2.text forKey:@"taxidiudiuprice"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/CarWash/upload",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"添加商品res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
